package com.hq.learnmybatisplus;

import com.alibaba.fastjson.JSON;
import com.hq.learnmybatisplus.config.SpringContextUtils;
import com.hq.learnmybatisplus.dao.UserDao;
import com.hq.learnmybatisplus.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class DuoShuJuYuan {
    static String resource;
    static SqlSessionFactory sqlSessionFactory;
    static InputStream inputStream;
    static SqlSession sqlSession;

    static {
        resource = "mybatis-config.xml";

        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //new Connection()
        //这里就获取到了数据源和sql
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void duo() throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/chunyu_live?useSSL=false&serverTimezone=Asia/Shanghai",
                "root", "root");
        SqlSession sqlSession = sqlSessionFactory.openSession(connection);
        //sqlSession.insert()
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> users = mapper.selectList(null);
        for (User user : users) {
            System.out.println(JSON.toJSONString(user));
        }
        System.out.println(mapper.selectList(null));
    }

    //SqlSessionManager 实现了Session接口。意味着，SqlSessionManager集成了 sqlSessionFactory和session 的功能。通过SqlSessionManager，
    // 开发者可以不在理会SqlSessionFacotry的存在，直接面向Session编程。
    @Autowired
    UserDao userDao;
    @Test
    public void test2() throws SQLException {
        //设置一个数据源
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://localhost:3306/chunyu_live?useSSL=false&serverTimezone=Asia/Shanghai");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("root");
        DataSource dataSource = dataSourceBuilder.build();
        //获取mybatisplus中的DefaultSqlSessionFactory
        DefaultSqlSessionFactory bean = SpringContextUtils.getBean(DefaultSqlSessionFactory.class);
        Configuration configuration = bean.getConfiguration();
        configuration.setEnvironment(new Environment("1515",  new ManagedTransactionFactory(),dataSource));

        //构建一个新的 SqlSessionFactory
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(configuration);
        SqlSession sqlSession1 = build.openSession();
        //test数据
        UserDao mapper = sqlSession1.getMapper(UserDao.class);
        List<User> users = mapper.selectList(null);
        for (User user : users) {
            System.out.println(JSON.toJSONString(user));
        }
        List<User> users1 = userDao.selectList(null);
        for (User user : users1) {
            System.out.println(JSON.toJSONString(user));
        }
    }

}
